// 上传图片，文件时候 处理文件保存路径的动态目录

const fs = require('fs')
const path = require('path')

exports.mkdirs = (pathname,callback)=>{

    pathname = path.isAbsolute(pathname)?pathname:path.join(__dirname,pathname);

    pathname = path.relative(__dirname,pathname);

    let floders = pathname.split(path.sep); // path.sep 避免平台带来的差异
    let pre = ""
    floders.forEach(floder => {

        try {
            let _stat = fs.statSync(path.join(__dirname,pre,floder));
            let hasMkdir = _stat && _stat.isDirectory();
            if(hasMkdir){
                callback
            }
            
        } catch (error) {
            try {
                fs.mkdirSync(path.join(__dirname,pre,floder));
                callback && callback(null)
            } catch (error) {
                callback && callback(error)
            }
        }

        pre = path.join(pre,floder); // 路径拼接
        
    });

}